<?php

namespace Utuo\TagLib;

use Think\Template\TagLib;

class Article extends TagLib {
	
	/**
	 * 定义标签列表
	 *
	 * @var array
	 */
	protected $tags = array(
			'list' => array(
					'attr' => 'id,category,paginate,pagesize',
					'close' => 1 
			), // 获取指定分类列表
			'page' => array(
					'attr' => '',
					'close' => 0 
			),
			'detail' => array(
					'attr' => 'id,arcid',
					'close' => 1 
			),
			'catlist' => array(
					'attr' => 'id,pid',
					'close' => 1 
			), // 文章栏目列表
			'cat' => array(
					'attr' => '',
					'close' => 1 
			)  // 文章栏目信息
		);
	
	// 文章栏目列表
	public function _catlist($tag, $content) {
		$pid = empty($tag['pid']) ? 0 : $tag['pid'];
		dump($tag);
		$parse = <<<'PARSE'
		<?php
			$condition = array(
					'status' => 1,
					'is_deleted' => 0,
					'cid' => $sid,
					'pid' => %d 
			);
			$__CAT_LIST__ = D('ArticleCategory')->where($condition)->select();
				var_dump($sid);
		?>
		<volist name="__CAT_LIST__" id="%d">
		%s
		</volist>
PARSE;
		return sprintf($parse, $pid, $tag['id'], $content);
	}
	
	/**
	 * 文章列表
	 *
	 * @param array $tags
	 * @param string $content
	 */
	public function _list($tag, $content) {
		$p = I("p", 1, "intval");
		$id = $tag['id'];
		$cate = $tag['category'] ? $tag['category'] : 0;
		if (!$cate)
			$cate = I('cat', 0, 'intval');
		$table_tag = 'article';
		$pagesize = $tag['pagesize'] ? $tag['pagesize'] : 10;
		$paginate = $tag['paginate'] ? '1' : '0';
		$tt = <<<'EOT'
		<?php
			$list = D('CommonContent')->realModel({4})->content_list({0}, {1}, {2}, {3});
			if({1}){
				$page = new \Think\Page($list['count'], {2}, $parameter = null);
				$pagination = $page->show();
			}
			$__LIST__ = $list['data'];
			foreach ($__LIST__ as &$v){
				$v['url'] = get_content_url({4}, $sid, $s, $v['id']);
			}
		?>
		<volist name="__LIST__" id="{5}">
		{6}
		</volist>				
EOT;
		$parse = \Utuo\String::format($tt, $cate, $paginate, $pagesize, $p, $table_tag, $id, $content);
		return $parse;
	}
	public function _page($tag, $content) {
		return <<<'EOT'
		<?php
			echo $pagination;
		?>
EOT;
	}
	
	/**
	 * 内容详细
	 *
	 * @param unknown $tag
	 * @param unknown $content
	 * @return string
	 */
	public function _detail($tag, $content) {
		$id = $tag['id'];
		$arcid = $tag['arcid'];
		$table_tag = 'article';
		if (!$arcid)
			$arcid = I('arc', 0, 'intval');
		$tt = <<<'EOT'
		<?php
			$__CONTENT__ = array();
			$arcid = {0};
			if($content && ($content['id']==$arcid)) {
				$__CONTENT__[] = $content;
			}elseif($arcid) {
				$list = D('CommonContent')->realModel({1})->content_detail($arcid);
				$list['data'] && $__CONTENT__[] = $list['data'];
			}
		?>
		<volist name="__CONTENT__" id="{2}">
		{3}
		</volist>
EOT;
		$parse = \Utuo\String::format($tt, $arcid, $table_tag, $id, $content);
		return $parse;
	}
}